package com.cmcc.cmii.smsmail.listener;

import com.cmcc.cmii.common.utils.JsonUtil;
import com.cmcc.cmii.smsmail.constants.MailMqConstants;
import com.cmcc.cmii.smsmail.model.v1.SimpleMail;
import com.cmcc.cmii.smsmail.service.IEmailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author Thanks
 */
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class MailSimpleListener {

    private final IEmailService iEmailService;


    /**
     * 处理简单邮件的发送
     * @param jsonStr
     */
    @RabbitListener(bindings = @QueueBinding(value = @Queue(value = MailMqConstants.MAIL_SIMPLE_QUEUE, durable = "true"),
            exchange = @Exchange(value = MailMqConstants.MAIL_SIMPLE_EXCHANGE,
                    ignoreDeclarationExceptions = "true",
                    type = ExchangeTypes.TOPIC, durable = "true"), key = MailMqConstants.MAIL_SIMPLE_KEY))
    public void handle(String jsonStr){
        try{
            SimpleMail simpleMail = (SimpleMail)JsonUtil.getModelFromJson(jsonStr, SimpleMail.class);
            iEmailService.sendSimpleMail(simpleMail);
        }catch (Exception e){
            e.printStackTrace();
            log.error("*************简单邮件发送出现未知异常,接收数据为:::***************{}",jsonStr);
        }

    }


}
